package com.yiji.fintech.musfinance.common.enginerules.base;

import com.acooly.core.utils.Collections3;
import com.yiji.fintech.musfinance.common.annotation.RuleNameAnnotation;
import com.yiji.fintech.musfinance.common.domain.channel.ChannelDomain;
import com.yiji.fintech.musfinance.common.domain.channel.InstChannelApiDomain;
import com.yiji.fintech.musfinance.common.exception.AbortBizException;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * Created by ouwen@yiji.com} on 2017/9/22.
 */
@Slf4j
public abstract class BaseFilter {

    public List<InstChannelApiDomain> channelApiList;

    public void doExcute(ChannelDomain domain){
        channelApiList = domain.getInstChannelApis();
        RuleNameAnnotation ruleNameAnnotation = this.getClass().getAnnotation(RuleNameAnnotation.class);
        String ruleName = ruleNameAnnotation.value();
        if(Collections3.isEmpty(channelApiList)){
            throw new AbortBizException("未找到匹配的路由规则");
        }
        log.info("GID【{}}】外部流水号【{}】内部流水号【{}】"+ruleName+"前渠道【{}】",domain.getGid(),domain.getMerchOrderNo(),domain.getBizOrderNo(),channelApiList);
        filter(domain);
        log.info("GID【{}}】外部流水号【{}】内部流水号【{}】"+ruleName+"后渠道【{}】",domain.getGid(),domain.getMerchOrderNo(),domain.getBizOrderNo(),channelApiList);
        if(Collections3.isEmpty(channelApiList)){
            throw new AbortBizException("未找到匹配的路由规则");
        }
    }

    /**
     * 过滤路由规则
     * @param domain
     */
    protected abstract void filter(ChannelDomain domain);
}
